//三元运算
function sayName(name) {
    // name = name == undefined ? 'lily' : name 
    if (name == undefined) {
        name = 'lily'
    }

    console.log(name)
}

// switch (3) {
//     case 1: console.log('a');break;
//     case 2: console.log('b');break;
//     case 3: console.log('c');break;
//     default: console.log('d');
// }
let i = 1
// for (; ; ) {
//     console.log(i)
//     i++
//     if(i>100){break}

// }
// while(i<=100){
//     console.log(i)
//     i++
// }

var str = "这是一个\n分行写的\n字符串";
var str1 = `这是一个
分行写的
字符串`;
function getClassName() {
    return 'linka'
}
var url = 'http://:baidu.com'
var calssName = 'link'
var link = "<a href='" + url + "' class='" + calssName + "'></a>"
var link2 = `<a href='${url}' class='${getClassName()}'>`

var arr = [1, 2, 3, 3, 2, 1, 2, 5]

function foo(arr) {
    let set = new Set(arr)
    return [...set]
}
//数组合并
var arr1 = [1, 2, 3]
var arr2 = [2, 3, 4]
var arr3 = arr1.concat(arr2)
var arr4 = [...arr1, ...arr2]

var obj1 = { 'name': 'lily', age: 20 }
var obj2 = { addr: '成都' }
var obj3 = { gender: '女' }
//对象合并
function concatObj() {
    //生成一个空对象
    let obj = {}
    //使用arguments收集所有形参
    for (let i = 0; i < arguments.length; i++) {
        //依次遍历每一个传入的对象
        let curObj = arguments[i]
        //遍历当前对象的键，根据键拿到对象的值
        for (let key in curObj) {
            obj[key] = curObj[key]
        }
    }
    return obj
}
function concatObj1() {
    //生成一个空对象
    let obj = {}
    //使用arguments收集所有形参
    for (let i = 0; i < arguments.length; i++) {
        //依次遍历每一个传入的对象
        let curObj = arguments[i]
        //解构obj，解构curObj,生成一个新的对象赋值给obj
        obj = { ...obj, ...curObj }
    }
    return obj
}
concatObj(obj1, obj2, obj3)

var stus = [
    { stuNum: 1101, name: '曹操', score: 95 },
    { stuNum: 1102, name: '吕布', score: 50 },
    { stuNum: 1103, name: '貂蝉', score: 65 },
    { stuNum: 1104, name: '赵云', score: 70 }
];
stus.sort(function (a, b) {
    return a.score - b.score
})

// 数组迭代
var arr5 = [2, 3, 4, 5, 6, 7, 8, 9]
var resForEach = arr5.forEach(function (item, index) {
    // console.log(item)
    // console.log(index)
    // console.log(arr)
    return 'a'
})
var resMap = arr5.map(function (item, index, arr) {
    // console.log(item)
    // console.log(index)
    // console.log(arr)
    return 'a'
})
//生成一个新的数组，数组每个元素的值是之前数组的两倍
function double(arr) {
    let res = []
    arr.forEach(function (item) {
        res.push(item * 2)
    })
    return res
}

function double1(arr) {
    let res = arr.map(function (item) {
        return item * 2
    })
    return res
}
var arr6 = [1, 2, 3, 4]
// var res6 = arr6.reduce(function(prev,cur,curIndex,array){
//     console.log(prev, cur, curIndex,array);
//     return prev + cur
// },0)
// var res7 = arr6.reduce(function(prev,cur,curIndex,array){
//     console.log(prev, cur, curIndex,array);
//     return prev * cur
// },1)
//计算数组里面每个元素出现的次数
let arr8 = ['a', 'b', 'c', 'a', 'c', 'a']
// res8 = {'a':3,'b':1,'c':2}
function countArray(arr) {
    let res = {}
    arr8.forEach(function (item) {
        if (item in res) {
            res[item]++
        } else {
            res[item] = 1
        }
    })
    return res
}

function countArray1(arr) {
    return arr.reduce(function (prev, current) {
        if (current in prev) {
            prev[current]++
        } else {
            prev[current] = 1
        }
        return prev
    }, {})
}
//函数递归
var n = 5;
function f(n) {
    if (n == 1) {
        return 1
    } else {
        return f(n - 1) * n
    }
}
var arr = [4,[1,2,['a','b'],3],5,[4,5]]

function flat(arr,res=[]){
    for(let i of arr){
        if(!Array.isArray(i)){
            res.push(i)
        }else{
            flat(i,res)
        }
    }
    return res
}

